/**
 * 
 */
package denaris.core;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;

/**
 * Represents the base for a particle system. Particle systems are attached to
 * entity groups and are responsible for creating bullets and particles. A
 * particle system contains an arbitrary number of particle emitters.
 * 
 * @author stkl23
 * 
 */
public abstract class AbstractParticleSystem {
	private Vector emitters = new Vector();

	public void addEmitter(AbstractParticleEmitter emitter) {
		emitters.addElement(emitter);
	}

	public void emitt(int x, int y) {
		Enumeration e = emitters.elements();
		while (e.hasMoreElements()) {
			AbstractParticleEmitter emitter = (AbstractParticleEmitter) e
					.nextElement();
			if (emitter.isActive()) {
				emitter.emitt(x, y);
			}
		}
	}

	public void paint(Graphics g) {
		Enumeration e = emitters.elements();
		while (e.hasMoreElements()) {
			AbstractParticleEmitter emitter = (AbstractParticleEmitter) e
					.nextElement();
			emitter.paint(g);
		}
	}

	public void update() {
		Enumeration e = emitters.elements();
		while (e.hasMoreElements()) {
			AbstractParticleEmitter emitter = (AbstractParticleEmitter) e
					.nextElement();
			emitter.update();
		}
	}

	public Vector getEmitters() {
		return emitters;
	}

	/**
	 * Shifts all active particles of this system into the given direction... vertical scrolling occurs when the map is too high for the display
	 * @param dy
	 */
	public void scrollVertical(int dy) {
		// iterate over all emitters and move their particles
		for (int i=0; i<emitters.size(); i++) {
			AbstractParticleEmitter e = (AbstractParticleEmitter) emitters.elementAt(i);
			Enumeration en = e.elements();
			while (en.hasMoreElements()) {
				AbstractParticle p = (AbstractParticle) en.nextElement();
				p.getController().move(0, dy);
			}
		}
	}
	
	public void reset() {
		// reset all emitters
		Enumeration e = emitters.elements();
		while (e.hasMoreElements()) {
			AbstractParticleEmitter emitter = (AbstractParticleEmitter) e.nextElement();
			emitter.reset();
		}
	}
}
